/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.Solicitud;

import BD.BaseDatos;
import com.myapp.struts.Carrera.Carrera;
import java.sql.ResultSet;
import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Andreina Wilhelm
 */
public class SeleccionarCarrerasUSBAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        // crea instancia de BD
        BaseDatos bd = new BaseDatos();

        try {
            // establece conexion con BD
            bd.establecerConexion();

            // se crea el query para obtener el id de USB
            String query = "SELECT id FROM institucion WHERE nombre='Universidad Simón Bolívar' AND sede='Caracas' AND activo='true'";

            // se ejecuta query
            ResultSet rs = bd.ejecutarQuery(query);
            if (rs.next()) {
                String idInst = rs.getString("id");

                // se crea el query para obtener las carreras
                query = "SELECT * FROM carrera WHERE activo='true' AND tipo='Larga' AND idInstitucion='" + idInst + "'ORDER BY nombre";

                // se ejecuta query
                rs = bd.ejecutarQuery(query);

                // se crea lista
                LinkedList<Carrera> carreraList = new LinkedList<Carrera>();

                while (rs.next()) {
                    Carrera car = new Carrera(rs.getString("codigo"), rs.getString("nombre"), Integer.parseInt(idInst), rs.getString("tipo"));
                    carreraList.add(car);
                }

                // Se agrega la lista al form 
                SeleccionarCarrerasUSBForm scf = (SeleccionarCarrerasUSBForm) form;
                scf.setCarrerasList(carreraList);

                // Se termina conexion con BD
                bd.terminarConexion();

                return mapping.findForward(SUCCESS);
            }

        } catch (Exception e) {
            System.out.println("ERROR: Problema al consultar la BD");
        }

        // Se termina conexion con BD
        bd.terminarConexion();

        return mapping.findForward(FAILURE);
    }
}
